/*
 * @Author: CQJ
 * @Date: 2022-09-08 01:22:11
 * @LastEditors: CQJ
 * @LastEditTime: 2022-09-08 01:36:23
 * @Description:
 */
import { reactive, ref } from 'vue'
export interface chaptersItem {
  article_content: string
  chapter_no: string
  chapter_name: string
  is_edit: boolean
  randomKey?: number
}
interface IArticleTags {
  tag_id: string
  tag_name: string
}

export interface IFormState {
  article_name: string
  article_categ_id: string | undefined
  article_subcateg_id: string | undefined
  article_album_ids: string[] | undefined
  publish_state: number
  article_author: string | undefined
  article_introduction: string | undefined
  article_cover_horizontal: string | undefined
  article_cover_horizontal_type: number
  article_cover_vertical: string | undefined
  article_cover_vertical_type: number
  article_status: number
  file_type: number
  article_id: string | undefined
  chapters: chaptersItem[]
  type_ids: string[] | undefined
  article_price: string | undefined
  article_price_type: number
  site_id: string | undefined

  article_keyword: string | undefined
  article_tags: IArticleTags[] | undefined
  article_tags_copy: IArticleTags[] | undefined
  article_pop_compensated: number
  article_pop_add_star: number
  article_pop_add_total: number
  article_pop_add_day: number
  article_pop_add_week: number
  article_pop_add_month: number
  article_pop_add_people_count: number
  article_pop_add_view: number
  article_pop_add_like: number
  article_pop_add_collection: number
  article_pop_add_share_count: number
  article_pop_star: number
  article_pop_total: number
  article_pop_day: number
  article_pop_week: number
  article_pop_month: number
  article_pop_people_count: number
  article_pop_view: number
  article_pop_like: number
  article_pop_collection: number
  article_pop_share_count: number
}

// 状态管理
export const formState = reactive<IFormState>({
  article_name: '',
  article_categ_id: undefined,
  article_subcateg_id: undefined,
  article_album_ids: [],
  publish_state: 2,
  article_author: '',
  article_introduction: '',
  article_cover_horizontal: '',
  article_cover_horizontal_type: 1,
  article_cover_vertical: '',
  article_cover_vertical_type: 1,
  file_type: 1,
  article_id: undefined,
  article_status: 1,
  chapters: [
    {
      chapter_no: '1',
      chapter_name: '',
      article_content: '',
      is_edit: false,
      randomKey: 0
    }
  ],
  type_ids: [],
  article_price: undefined,
  article_price_type: 1,
  site_id: undefined,
  article_keyword: undefined,
  article_tags: undefined,
  article_tags_copy: undefined,
  article_pop_compensated: 2,
  article_pop_star: 0,
  article_pop_total: 0,
  article_pop_day: 0,
  article_pop_week: 0,
  article_pop_month: 0,
  article_pop_people_count: 0,
  article_pop_view: 0,
  article_pop_like: 0,
  article_pop_collection: 0,
  article_pop_share_count: 0,
  article_pop_add_star: 0,
  article_pop_add_total: 0,
  article_pop_add_day: 0,
  article_pop_add_week: 0,
  article_pop_add_month: 0,
  article_pop_add_people_count: 0,
  article_pop_add_view: 0,
  article_pop_add_like: 0,
  article_pop_add_collection: 0,
  article_pop_add_share_count: 0
})
// interface ExtendInfo {
//   article_keyword: string | undefined
//   article_tags: IArticleTags[] | undefined
//   article_tags_copy: IArticleTags[] | undefined
//   article_pop_compensated: number
//   article_pop_add_star: number
//   article_pop_add_total: number
//   article_pop_add_day: number
//   article_pop_add_week: number
//   article_pop_add_month: number
//   article_pop_add_people_count: number
//   article_pop_add_view: number
//   article_pop_add_like: number
//   article_pop_add_collection: number
//   article_pop_add_share_count: number
//   article_pop_star: number
//   article_pop_total: number
//   article_pop_day: number
//   article_pop_week: number
//   article_pop_month: number
//   article_pop_people_count: number
//   article_pop_view: number
//   article_pop_like: number
//   article_pop_collection: number
//   article_pop_share_count: number
// }
export interface ClearExtendInfo {
  article_pop_add_total: number
  article_pop_add_day: number
  article_pop_add_week: number
  article_pop_add_month: number
  article_pop_add_people_count: number
  article_pop_add_view: number
  article_pop_add_like: number
  article_pop_add_collection: number
  article_pop_add_share_count: number
  article_pop_add_star: number
}
// export const extendInfo = reactive<ExtendInfo>({
//   article_keyword: undefined,
//   article_tags: undefined,
//   article_tags_copy: undefined,
//   article_pop_compensated: 2,
//   article_pop_add_star: 3,
//   article_pop_add_total: 0,
//   article_pop_add_day: 0,
//   article_pop_add_week: 0,
//   article_pop_add_month: 0,
//   article_pop_add_people_count: 0,
//   article_pop_add_view: 0,
//   article_pop_add_like: 0,
//   article_pop_add_collection: 0,
//   article_pop_add_share_count: 0,
//   article_pop_star: 3,
//   article_pop_total: 0,
//   article_pop_day: 0,
//   article_pop_week: 0,
//   article_pop_month: 0,
//   article_pop_people_count: 0,
//   article_pop_view: 0,
//   article_pop_like: 0,
//   article_pop_collection: 0,
//   article_pop_share_count: 0
// })
//随机生成区间范围数
const selectFrom = (lowerValue: number, upperValue: number) => {
  let choices = upperValue - lowerValue + 1
  return Math.floor(Math.random() * choices + lowerValue)
}
//判断是否是小数
const isDot = (num: number) => {
  if (String(num).indexOf('.') > -1) {
    return true
  } else {
    return false
  }
}
//以0.5为一个step向上取整
const floorFunc = (num: number) => {
  if (isDot(num)) {
    const int = parseInt(num.toString())
    const floatNum = num - int
    if (floatNum < 0.5) {
      return int + 0.5
    } else {
      return int + 1
    }
  }
  return num
}
//重置扩展信息
export const resetExtendFunc = () => {
  const form = {
    article_pop_add_total: formState.article_pop_add_total,
    article_pop_add_day: formState.article_pop_add_day,
    article_pop_add_week: formState.article_pop_add_week,
    article_pop_add_month: formState.article_pop_add_month,
    article_pop_add_people_count: formState.article_pop_add_people_count,
    article_pop_add_view: formState.article_pop_add_view,
    article_pop_add_like: formState.article_pop_add_like,
    article_pop_add_collection: formState.article_pop_add_collection,
    article_pop_add_share_count: formState.article_pop_add_share_count,
    article_pop_add_star: formState.article_pop_add_star
  }
  let key: keyof ClearExtendInfo
  for (key in form) {
    formState[key] = 0
  }
}
//随机补偿方法
export const randomFunc = () => {
  const data = {
    article_pop_add_total: 0,
    article_pop_add_day: 0,
    article_pop_add_week: 0,
    article_pop_add_month: 0,
    article_pop_add_people_count: 0,
    article_pop_add_view: 0,
    article_pop_add_like: 0,
    article_pop_add_collection: 0,
    article_pop_add_share_count: 0,
    article_pop_add_star: 0
  }
  data.article_pop_add_view = selectFrom(1000, 5000) //总浏览
  const monthView = +(data.article_pop_add_view / 2).toFixed(0) //月浏览
  const weekView = +(data.article_pop_add_view / 8).toFixed(0) //周浏览
  const dayView = +(data.article_pop_add_view / 60).toFixed(0) //日浏览
  data.article_pop_add_like = selectFrom(10, 600) //总点赞
  const monthLike = +(data.article_pop_add_like / 2).toFixed(0) //月点赞
  const weekLike = +(data.article_pop_add_like / 8).toFixed(0) //周点赞
  const dayLike = +(data.article_pop_add_like / 60).toFixed(0) //日点赞
  data.article_pop_add_collection = selectFrom(10, 600) //总收藏
  const monthCollection = +(data.article_pop_add_collection / 2).toFixed(0) //月收藏
  const weekCollection = +(data.article_pop_add_collection / 8).toFixed(0) //周收藏
  const dayCollection = +(data.article_pop_add_collection / 60).toFixed(0) //日收藏
  data.article_pop_add_share_count = selectFrom(10, 600) //总分享
  const monthShare = +(data.article_pop_add_share_count / 2).toFixed(0) //月分享
  const weekShare = +(data.article_pop_add_share_count / 8).toFixed(0) //周分享
  const dayShare = +(data.article_pop_add_share_count / 60).toFixed(0) //日分享
  //总人气
  data.article_pop_add_total =
    data.article_pop_add_view +
    100 * data.article_pop_add_like +
    100 * data.article_pop_add_collection +
    100 * data.article_pop_add_share_count
  //月人气
  data.article_pop_add_month = monthView + 100 * monthLike + 100 * monthCollection + 100 * monthShare
  //周人气
  data.article_pop_add_week = weekView + 100 * weekLike + 100 * weekCollection + 100 * weekShare
  //日人气
  data.article_pop_add_day = dayView + 100 * dayLike + 100 * dayCollection + 100 * dayShare
  //随机评分，用于计算星级
  const score = selectFrom(6.0, 10.0)
  //星级
  data.article_pop_add_star = floorFunc(score / 2)
  //总评分人数
  data.article_pop_add_people_count = selectFrom(1000, 2000)
  console.log('score: ', score.toFixed(1))
  console.log('article_pop_add_star: ', data.article_pop_add_star.toFixed(1))
  Object.assign(formState, data)
  console.log('formState: ', formState)
}

export const isPopularity = ref(false)
